package com.iss.umservice.common.xml;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;


public class XmlParser
{
    /**
     * @param xmlfile   XML
     * @return XmlNode
     * @throws SAXException SAXException
     * @throws ParserConfigurationException ParserConfigurationException
     * @throws IOException IOException
     */
    public static XmlNode parserXML(String xmlfile) throws SAXException,
            ParserConfigurationException, IOException
    {
        if (null == xmlfile)
        {
            return null;
        }
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();
        XmlHandler handler = new XmlHandler();
        File XMLFile = new File(xmlfile);
        if (!XMLFile.exists())
        {
            return new XmlNode();
        }
        
        parser.parse(XMLFile, handler);
        
        return handler.getRootNode();
    }
    
    /**
     * @param source InputSource
     * @return XmlNode
     * @throws SAXException SAXException
     * @throws ParserConfigurationException ParserConfigurationException
     * @throws IOException IOException
     */
    public static XmlNode parserXML(InputSource source) throws SAXException,
            ParserConfigurationException, IOException
    {
        if (null == source)
        {
            return null;
        }
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();
        XmlHandler handler = new XmlHandler();
        parser.parse(source, handler);
        
        return handler.getRootNode();
    }
    
    /**
     * @return XmlNode
     * @throws SAXException SAXException
     * @throws ParserConfigurationException ParserConfigurationException
     * @throws IOException IOException
     */
    public static XmlNode parserXML(InputStream stream) throws SAXException,
            ParserConfigurationException, IOException
    {
        if (null == stream)
        {
            return null;
        }
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();
        XmlHandler handler = new XmlHandler();
        parser.parse(stream, handler);
        
        return handler.getRootNode();
    }
    
    /**
     * @return XmlNode
     * @throws SAXException SAXException
     * @throws ParserConfigurationException ParserConfigurationException
     * @throws IOException IOException
     */
    public static XmlNode parserXMLString(String xmlString)
            throws SAXException, ParserConfigurationException, IOException
    {
        if (null == xmlString || "".equals(xmlString))
        {
            return null;
        }
        
        StringReader sread = new StringReader(xmlString);
        InputSource is = new InputSource(sread);
        
        return parserXML(is);
    }
    
    public static void main(String[] args) throws SAXException,
            ParserConfigurationException, IOException
    {

        InputStream is = XmlParser.class.getResourceAsStream("/gzt.xml");
        XmlNode node = XmlParser.parserXML(is);
        System.out.println(node.getName());
        
        XmlNode sysNode = node.getSubNode("system", 0);
        System.out.println(sysNode.getName());
        
        XmlNode xmlNode = sysNode.getSubNode("pageSize", 0);
        System.out.println(xmlNode.getName());
        
        StringBuffer sb = new StringBuffer();
        //        sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        sb.append("<modules>");
        sb.append("<system>�й�</system>");
        sb.append("</modules>");
        XmlNode nodeString = XmlParser.parserXMLString(sb.toString());
        System.out.println(nodeString.getName());
        System.out.println(nodeString.getSubNodes().get(0).getValue());
        System.out.println(nodeString.toXML());
    }
}